/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bingSearch;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.ArrayList;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

/**
 *
 * @author Asep Sucipto
 */
public class Search {
    public static ArrayList<String> search(String query){
       
      //  String[] links= new String [4];
      System.out.println( "Searching for Query: " + query );
      InputStream is = Request.BuildRequest( query, 10);
      ArrayList hasil= new ArrayList<String>();
//       String result = Request.streamToString(is);
//      System.out.println("output \n" + result);
//       Request.stringToFile(result, "./src/outputs/" + query+".xml");
        
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        try {
		SAXParser saxParser = saxParserFactory.newSAXParser();
	        BingAPIHandler handler = new BingAPIHandler();
	        //saxParser.parse(new File("./src/main/outputs/paris.xml"), handler);
	        saxParser.parse(is, handler);
	        List<Entry> entries = handler.getEntries();
	        String s = ""; 
	        for(Entry entry: entries){
	        	//s += query + "\t" + entry.getDisplayUrl() + "\t" + entry.getUrl() + "\t" + entry.getTitle() + "\n";
                    s += entry.getUrl();
                    hasil.add(entry.getUrl());
                    
	        }
	        System.out.println("Writing results to the file");
	      //  Request.stringToFile(s,"./src/outputs/results.txt");
	        
		} catch (ParserConfigurationException e) {
			//e.printStackTrace();
			//System.out.println(e.getMessage());
		} catch (SAXException e) {
			//e.printStackTrace();
			//System.out.println(e.getMessage());
                        
		} catch (IOException e) {
			//e.printStackTrace();
			//System.out.println(e.getMessage());

		}
        return hasil;
	}
 }


        